/*
    libhdvbd - Library for building sophisticated DVB applications
    Copyright (C) 2012 Tony Houghton <h@realh.co.uk>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/* Connects to an HdvbChannelScan's signals to add data to a database.
 * Automatically frees itself on cancelled or completed signals.
 */

#include "chanscan-factory.h"
#include "chansdb.h"
#include "freesatscantodb.h"
#include "hdvb-freesat-scan.h"

void
hdvb_freesat_db_creator(sqlite3 *sql, gpointer handle)
{
    hdvb_chans_db_create(sql, handle);
    hdvb_db_exec(sql,
            "CREATE TABLE region_codes ("
            "    code         INT NOT NULL,"
            "    lang         INT NOT NULL,"
            "    name         TEXT NOT NULL"
            ")");
}

void
hdvb_freesat_scan_to_db_delete(HdvbChanScanToDb *cs2db)
{
    HdvbFreesatScanToDb *fs2db = (HdvbFreesatScanToDb *) cs2db;
    
    if (fs2db->region_tag)
    {
        g_signal_handler_disconnect(cs2db->scanner, fs2db->region_tag);
        fs2db->region_tag = 0;
    }
    if (fs2db->region_stmt)
    {
        sqlite3_finalize(fs2db->region_stmt);
        fs2db->region_stmt = NULL;
    }
    hdvb_chan_scan_to_db_standard_delete(cs2db);
}

static void
hdvb_freesat_scan_to_db_region_cb(HdvbChannelScan *scanner,
        int code, int lang, const char *name, HdvbFreesatScanToDb *fs2db)
{
    sqlite3_stmt *stmt = fs2db->region_stmt;
                    
    sqlite3_bind_int(stmt, 1, code);
    sqlite3_bind_int(stmt, 2, lang);
    sqlite3_bind_text(stmt, 3, name, -1, SQLITE_TRANSIENT);
    sqlite3_step(stmt);
    sqlite3_clear_bindings(stmt);
    sqlite3_reset(stmt);
}

gboolean
hdvb_freesat_scan_to_db_init(HdvbFreesatScanToDb *fs2db,
        sqlite3 *db, const char *factory)
{
    if (!hdvb_chan_scan_to_db_init(&fs2db->parent, db, factory))
        return FALSE;
    fs2db->parent.del = hdvb_freesat_scan_to_db_delete;
    fs2db->region_stmt = hdvb_db_prepare_statement(db,
            "INSERT OR REPLACE INTO region_codes"
            "   (code, lang, name)"
            "   VALUES (?, ?, ?)");
    
    if (fs2db->region_stmt)
    {
        fs2db->region_tag = g_signal_connect(fs2db->parent.scanner,
                "region-discovered",
                (GCallback) hdvb_freesat_scan_to_db_region_cb, fs2db);
    }
    return fs2db->region_stmt != NULL;
}

HdvbChanScanToDb *
hdvb_freesat_scan_to_db_new(sqlite3 *db, const char *factory)
{
    HdvbFreesatScanToDb *fs2db = g_new0(HdvbFreesatScanToDb, 1);
    
    if (!hdvb_freesat_scan_to_db_init(fs2db, db, factory))
    {
        hdvb_freesat_scan_to_db_delete(&fs2db->parent);
        return NULL;
    }
    return &fs2db->parent;
}
